%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     MySQL 接口
%%% @end
%%%-------------------------------------------------------------------
-module(db_mysql).

-export([
    query/1
    ,query/2
    ,select/1
    ,select/2
    ,select_one/1
    ,select_one/2
    ,select_map/1
    ,select_map/2
    ,select_map_one/1
    ,select_map_one/2
    ,insert/1
    ,insert/2
    ,insert2/1
    ,insert2/2
    ,update/1
    ,update/2
    ,update2/1
    ,update2/2
    ,delete/1
    ,delete/2

    ,error_message/1
]).

-include("common.hrl").

-define(name, mysql_core).


%%%===================================================================
%%% API
%%%===================================================================

query(Query) ->
    mysql:query(?name, Query).

query(Query, Params) ->
    mysql:query(?name, Query, Params).

select(Query) ->
    select(Query, []).
select(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        {ok, _ColumnNames, Rows} -> {ok, Rows};
        {error, Reason} -> {error, error_message(Reason)}
    end.

select_one(Query) ->
    select_one(Query, []).
select_one(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        {ok, _ColumnNames, []} -> {ok, undefined};
        {ok, _ColumnNames, [Row]} -> {ok, Row};
        {error, Reason} -> {error, error_message(Reason)}
    end.

select_map(Query) ->
    select_map(Query, []).
select_map(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        {ok, ColumnNames, Rows} -> {ok, to_map(ColumnNames, Rows)};
        {error, Reason} -> {error, error_message(Reason)}
    end.

select_map_one(Query) ->
    select_map_one(Query, []).
select_map_one(Query, Params) ->
    case select_map(Query, Params) of
        {ok, []} -> {ok, undefined};
        {ok, [Row]} -> {ok, Row};
        {error, Reason} -> {error, error_message(Reason)}
    end.

insert(Query) ->
    insert(Query, []).
insert(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        ok ->
            LastInsertId = mysql:insert_id(?name),
            {ok, LastInsertId};
        {error, Reason} -> {error, error_message(Reason)}
    end.

insert2(Query) ->
    insert2(Query, []).
insert2(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        ok ->
            LastInsertId = mysql:insert_id(?name),
            AffectedRows = mysql:affected_rows(?name),
            {ok, LastInsertId, AffectedRows};
        {error, Reason} -> {error, error_message(Reason)}
    end.

update(Query) ->
    update(Query, []).
update(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        ok -> ok;
        {error, Reason} -> {error, error_message(Reason)}
    end.

update2(Query) ->
    update2(Query, []).
update2(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        ok ->
            AffectedRows = mysql:affected_rows(?name),
            {ok, AffectedRows};
        {error, Reason} -> {error, error_message(Reason)}
    end.

delete(Query) ->
    delete(Query, []).
delete(Query, Params) ->
    case mysql:query(?name, Query, Params) of
        ok -> ok;
        {error, Reason} -> {error, error_message(Reason)}
    end.


error_message({_Code, _SQLState, Message}) ->
    Message;
error_message(Message) ->
    Message.


%%%===================================================================
%%% Internal functions
%%%===================================================================

to_map(Fields0, Datas) ->
    Fields = [util_type:to_atom(Field) || Field <- Fields0],
    [maps:from_list(lists:zip(Fields, Data)) || Data <- Datas].
